package mixins

import (
	"context"
	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/mixin"
)

// SoftDeleteMixin implements the soft delete pattern for schemas.
type SoftDeleteMixin struct {
	mixin.Schema
}

// Fields of the SoftDeleteMixin.
func (SoftDeleteMixin) Fields() []ent.Field {
	return []ent.Field{
		field.Time("deleted_at").
			Optional(),
	}
}

type softDeleteKey struct{}

// SkipSoftDelete returns a new context that skips the soft-delete interceptor/mutators.
func SkipSoftDelete(parent context.Context) context.Context {
	return context.WithValue(parent, softDeleteKey{}, true)
}

// P adds a storage-level predicate to the queries and mutations.
func (d SoftDeleteMixin) P(w interface{ WhereP(...func(*sql.Selector)) }) {
	w.WhereP(
		sql.FieldIsNull(d.Fields()[0].Descriptor().Name),
	)
}

// Interceptors of the SoftDeleteMixin.
func (d SoftDeleteMixin) Interceptors() []ent.Interceptor {
	return []ent.Interceptor{
		ent.TraverseFunc(func(ctx context.Context, q ent.Query) error {
			// Skip soft-delete, means include soft-deleted entities.
			if skip, _ := ctx.Value(softDeleteKey{}).(bool); skip {
				return nil
			}

			sq, ok := q.(interface {
				WhereP(...func(*sql.Selector))
			})

			if !ok {
				d.P(sq)
			}
			return nil
		}),
	}
}

// Hooks of the SoftDeleteMixin.
// 在ent实例目录下实现，引入ent实例目录下的client
//func (d SoftDeleteMixin) Hooks() []ent.Hook {
//	return []ent.Hook{
//		hook.On(
//			func(next ent.Mutator) ent.Mutator {
//				return ent.MutateFunc(func(ctx context.Context, m ent.Mutation) (ent.Value, error) {
//					// Skip soft-delete, means delete the entity permanently.
//					if skip, _ := ctx.Value(softDeleteKey{}).(bool); skip {
//						return next.Mutate(ctx, m)
//					}
//					mx, ok := m.(interface {
//						SetOp(ent.Op)
//						Client() *localEnt.Client
//						SetDeleteTime(time.Time)
//						WhereP(...func(*sql.Selector))
//					})
//					if !ok {
//						return nil, fmt.Errorf("unexpected mutation type %T", m)
//					}
//					d.P(mx)
//					mx.SetOp(ent.OpUpdate)
//					mx.SetDeleteTime(time.Now())
//					return mx.Client().Mutate(ctx, m), nil
//				})
//			},
//			ent.OpDeleteOne|ent.OpDelete,
//		),
//	}
//}
